﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using geetol.sms.core;

namespace geetol.sms.api
{
    public class test
    {
        public back _back;

        public test()
        {
           getLogs();
        }

        bool iscon()
        {
            core.DbHelperSQL.connectionString = core.config.LoadSqlConnection();
            if (string.IsNullOrEmpty(core.DbHelperSQL.connectionString))
            {
                MessageBox.Show("请选配置数据库连接");
                return false;
            }
            return true;
        }

        public void SendAddVIP()
        {
            if (!iscon()) return;

            string card_id = "gt_test_"+getRom(6);
            string sqlstr = " insert into pos_t_vip_info (oper_date,card_id,card_type,vip_name,vip_sex,vip_tel,card_balance,vip_integral,total_integral,ic_cardflag)" +
                " values (getdate(),'" + card_id + "','1','测试" + DateTime.Now.ToString("mmss") + "','男','15874257250'," + getRom(5) + "," + getRom(6) + "," + getRom(7) + ",1)";
            int res = core.DbHelperSQL.ExecuteSql(sqlstr);
            _back.addvip.Add(card_id);
            setLogs();
            MessageBox.Show("影响行数:" + res);
        }

        public void SendVipSto()
        {
            if (!iscon()) return;
            string sqlstr = "select top 1 card_id from pos_t_vip_info order by oper_date desc";
            object obj = DbHelperSQL.GetSingle(sqlstr);
            string vv = getRom(4);
            if (obj != null)
            {
                sqlstr = "insert into pos_t_vip_cost";
                sqlstr += "(oper_id , oper_date , card_id , card_cost , card_way , branch_no , actual_payamt , reverse_desc , other1 , remark , " +
                    "card_fullfront , card_fullback , trade_date) values ";
                sqlstr += "( '9001' , getdate() , '"+ vv + "' , "+getRom(4)+" , '充值' , '000001' , "+ vv + " , '充值:"+ vv + "实付:"+ vv + "付款方式:现金' , ";
            }
            else
            {
                MessageBox.Show("需要添加前置会员");
            }
        }


       public  void DeleteTestData()
        {
            if (!iscon()) return;
            int aa = 0, bb = 0, cc = 0;
            if (_back.addvip.Count > 0)
            {
                string sqlstr = "delete from [pos_t_vip_info] where card_id in (";
                foreach (var s in _back.addvip)
                {
                    sqlstr += "'" + s + "',";
                }
                sqlstr = sqlstr.TrimEnd(',');
                sqlstr += ")";
                _back.addvip.Clear();
                aa = DbHelperSQL.ExecuteSql(sqlstr);
            }

            setLogs();
            MessageBox.Show(string.Format("删除 新增会员：{0}  会员销售：{1}  会员储值：{2}",aa,bb,cc));
        }

        public void getLogs()
        {
            string s =  config.ReadFile("smstest.txt");
            if (string.IsNullOrEmpty(s))
                _back = new back();
            else
                _back = Serialize.FromJson<back>(s);
        }

        public void setLogs()
        {
            var s = Serialize.ToJson(_back);
            config.WriterFile("smstest.txt", s);
        }

        public class back
        {
            public List<string> addvip = new List<string>();
            public List<string> vipsale = new List<string>();
            public List<string> vipsto = new List<string>();
        }

        string getRom(int num)
        {
            Random rd = new Random();
            string s = "";
            for (int i = 0; i < num; i++)
            {
                s += rd.Next(0, 9);
            }
            return s;
        }
    }
}
